package com.cloud.service.sys;

import com.cloud.dao.MsgDao;
import com.cloud.dao.UserDao;
import com.cloud.enums.ReadState;
import com.cloud.model.Msg;
import com.cloud.model.QMsg;
import com.cloud.model.User;
import com.cloud.pojo.Result;
import com.cloud.util.CollectionUtil;
import com.cloud.util.SysUtil;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MsgService extends BaseService<Msg> {
    @Resource
    private MsgDao msgDao;
    @Resource
    private UserDao userDao;
    @Resource
    JPAQueryFactory jpaQueryFactory;

    @Override
    public JpaRepository getDao() {
        return msgDao;
    }

    public Result saveMsg(Msg entity) throws Exception {
        Result result = entity.validateField();
        if (!result.isSuccess()) {
            return result;
        }
        Msg tDb;
        if (SysUtil.isEmpty(entity.getId())) {
            //添加
            tDb = msgDao.save(entity);
        } else {
            //编辑
            Msg Msg = msgDao.getOne(entity.getId());
            Msg.initModifyFields(entity);
            tDb = msgDao.save(Msg);
        }
        return new Result(true, "成功", tDb);
    }

    public void saveMsg(String handler, String objId, String title, String handleAction, String content, String username) {
        if (!handler.equals(username)) {
            Msg msg = new Msg(objId, title, handleAction, content, username);
            msgDao.save(msg);
        }
    }


    public List<Msg> getUnReadMsg(String username, Integer limit) {
        User user = userDao.findByUsername(username);
        Date date = user.getReadMsgTime();
        if (date == null) {
            date = user.getCreateTime();
        }
        QMsg qMsg = QMsg.msg;
        JPAQuery<Msg> msgJPAQuery = jpaQueryFactory.selectFrom(qMsg).where(qMsg.username.eq(username), qMsg.createTime.gt(date)).orderBy(qMsg.createTime.desc());
        List<Msg> msgs = msgJPAQuery.fetch();
        return msgs;
    }

    public long getUnReadMsgCount(String username) {
        User user = userDao.findByUsername(username);
        Date date = user.getReadMsgTime();
        if (date == null) {
            date = user.getCreateTime();
        }
        QMsg qMsg = QMsg.msg;
        JPAQuery<Msg> msgJPAQuery = jpaQueryFactory.selectFrom(qMsg).where(qMsg.username.eq(username), qMsg.createTime.gt(date)).orderBy(qMsg.createTime.desc());
        return msgJPAQuery.fetchCount();
    }

    public void mark(String[] ids) {
        QMsg qMsg = QMsg.msg;
        JPAQuery<Msg> msgJPAQuery = jpaQueryFactory.selectFrom(qMsg).where(qMsg.id.in(ids));
        List<Msg> os = msgJPAQuery.fetch();
        if (CollectionUtil.isNotEmpty(os)) {
            for (Msg o : os) {
                o.setReadState(ReadState.YD);
            }
        }

        msgDao.saveAll(os);
    }
}
